package com.tyk.bookstore.back.comment.service.impl;

import com.tyk.bookstore.back.comment.model.dto.CommentNum4TaskDto;
import com.tyk.bookstore.back.comment.model.entity.CommentDo;
import com.tyk.bookstore.back.comment.model.entity.ReplyDo;
import com.tyk.bookstore.back.comment.repository.CommentRepository;
import com.tyk.bookstore.back.comment.repository.ReplyRepository;
import com.tyk.bookstore.back.comment.service.CommentApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author tyk
 * @description CommentApiServiceImpl
 */
@Service
@RequiredArgsConstructor
public class CommentApiServiceImpl implements CommentApiService {

    private final CommentRepository commentRepository;
    private final ReplyRepository replyRepository;

    @Override
    public List<CommentNum4TaskDto> getCommentNum4TaskDtoList() {
        return commentRepository.selectCommentNum4TaskDtoList();
    }

    @Override
    public void deleteByBookId(Long bookId) {
        Set<Long> cidSet = commentRepository.<BigInteger>selectSinList1Eq(
                CommentDo::getId, CommentDo::getBookId, bookId, CommentDo.class
        ).stream().map(it -> it.getV1().longValue()).collect(Collectors.toSet());
        commentRepository.delete1Eq(CommentDo::getBookId, bookId, CommentDo.class);
        replyRepository.delete1In(ReplyDo::getCommentId, cidSet, ReplyDo.class);
    }

}
